package com.erlang4j.internal.messages;

/**
 * <p>
 * A single token returned by the MessageLexicalAnalyser.
 * </p>
 * <p>
 * Design notes
 * <ul>
 * <li>It needs the raw string and position so that nice error message can be given
 * <li>this is a struct not an object. the fields and public final and immutable.
 * </ul>
 * </p>
 * 
 * @author Phil Rice
 */
public class MessageToken {
	public final MessageTokenType type;
	public final String value;
	public final String raw;
	public final int pos;

	public MessageToken(String raw, int pos, MessageTokenType type, String value) {
		this.raw = raw;
		this.pos = pos;
		this.type = type;
		this.value = value;
	}

	@Override
	public String toString() {
		return type + "/" + value;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + pos;
		result = prime * result + ((raw == null) ? 0 : raw.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MessageToken other = (MessageToken) obj;
		if (pos != other.pos)
			return false;
		if (raw == null) {
			if (other.raw != null)
				return false;
		} else if (!raw.equals(other.raw))
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}

}
